Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Types of Inheritance

Hybrid inheritance

Hybrid inheritance in Java is a combination of two or more types of inheritance. It’s a mixture of single, multiple, multilevel, and hierarchical inheritance. The main purpose of using hybrid inheritance is to modularize the code into well-defined classes and provide code reusability. Here’s an example of hybrid inheritance in Java, which is a combination of single and multiple inheritance through interfaces:
Basic example of hybrid inheritance in Java interface read { public void reading(); } interface work { public void working(); } class Employee implements read, work { String firstName, lastName; int age; public void reading() { System.out.println("Employee is reading !!"); } public void working() { System.out.println("Employee is working !!"); } } class HourlyEmployee extends Employee { int salary = 5000, hoursWorked; public int computePay() { return salary * hoursWorked; } } public class Main { public static void main(String[] args) { HourlyEmployee emp = new HourlyEmployee(); emp.hoursWorked = 12; emp.firstName = "John"; emp.lastName = "Smith"; emp.age = 27; System.out.println(emp.computePay()); } }

Output

60000
In this example, the Employee class implements the read and work interfaces, which is an example of multiple inheritance. The HourlyEmployee class extends the Employee class, which is an example of single inheritance. Therefore, this is an example of hybrid inheritance.

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops ★ inheritance ★ Hybrid inheritance

Tutorials